'use strict';

var nodeJieba = require('nodejieba');

module.exports = function participleFunc(opts) {
  if (opts) {
    var preload = opts.preload || true;

    if (preload) {
      nodeJieba.load(opts.userDict);
    }
  }

  var participle = {};

  /**
   * load userDict
   * @param  {String|Object} dict 'string'|{userDict: './userdict.utf8'}
   * @return {Object} nodeJieba.load(dict)
   */
  participle.load = function(dict) {
    if (typeof dict !== 'object') {
      dict = {
        'userDict': dict
      };
    }
    return nodeJieba.load(dict);
  };

  /**
   * cut text
   * @param  {String} txt         Text to be treated
   * @param  {String} algorithm   'MP|HMM|Mix|Full|Query'
   * @param  {Number} granularity granularity
   * @return {String|Array} text list
   */
  participle.cut = function(txt, algorithm, granularity) {
    return nodeJieba.cut(txt, algorithm, granularity);
  };

  /**
   * tag text
   * @param  {String} txt         Text to be treated
   * @return {String|Array} [text:type]
   */
  participle.tag = function(txt) {
    return nodeJieba.tag(txt);
  };

  /**
   * tag text
   * @param  {String} txt Text to be treated
   * @param  {String} algorithm 'MP|HMM|Mix|Full|Query'
   * @return {String|Array} [text:proportion]
   */
  participle.extract = function(txt, granularity) {
    return nodeJieba.extract(txt, granularity);
  };

  /**
   * insert text
   * @param  {String} txt Text to be treated
   * @return {Object} nodeJieba.insertWord(txt)
   */
  participle.insertWord = function(txt) {
    return nodeJieba.insertWord(txt);
  };

  return participle;
};
